package fsu.cs.cis5930;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context context;
	
	public HelloItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
	}
	
	public HelloItemizedOverlay(Drawable defaultMarker, Context context){
		this(defaultMarker);
		this.context = context;
	}	

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay){
		mOverlays.add(overlay);
		this.populate();
	}
	
	
	
	@Override
	protected boolean onTap(int index) {	
	  OverlayItem item = mOverlays.get(index);
	  
	  Geocoder geoCoder = new Geocoder(this.context, Locale.getDefault());
      try{
      	List<Address> addresses = geoCoder.getFromLocation((double)item.getPoint().getLatitudeE6()/1E6,(double) item.getPoint().getLongitudeE6()/1E6, 1);
      	String add = "";
      	if(addresses.size() > 0)
      	{
      	    for(int i = 0; i<addresses.get(0).getMaxAddressLineIndex();i++)
      	    {
      	    	add += addresses.get(0).getAddressLine(i) + "\n";
      	    }
      	  Toast.makeText(context, add, Toast.LENGTH_LONG).show();
      	}
      }
      	catch(IOException e){
      		e.printStackTrace();
      	}
      	
	  return true;
	}
	

}
